/*
 * @Author       : Hejh(3010733382@qq.com)
 * @Version      : V1.0
 * @Date         : 2024-02-04 21:17:43
 * @Description  : 后台权限导航菜单
 */

import type { IndexObj } from '@/types/commom'

// 根据用户权限进行菜单的过滤
// 1. 对于有children的，如果本身不通过，不push，不遍历children
//    通过：对子项目递归
// 2. 对于没有children的，看本身通不通过，本身通过，push，不通过，不push
// 通过标准，auth没有或者auth列表中包含用户权限
/**
 * 根据用户权限进行菜单的过滤
 * @param permissions 权限数组
 * @param menu 原始菜单
 * @returns 返回过滤后的菜单
 */
export const filterAdminMenu = (permissions: string[], menu: IndexObj[]) => {
  const isPass = (auth: string[]) => {
    if (auth === undefined) {
      return true
    }

    return permissions.some((item) => {
      return auth.includes(item)
    })
  }

  const deepFilter = (menuItem: IndexObj) => {
    let res = null
    if (isPass(menuItem.auth)) {
      res = menuItem
      // 看是否存在children
      if (menuItem.children && menuItem.children.length > 0) {
        const arrMenu: IndexObj[] = []
        menuItem.children.forEach((item: IndexObj) => {
          const resMenu = deepFilter(item)
          if (resMenu) {
            arrMenu.push(resMenu)
          }
        })
        menuItem.children = arrMenu
      }
    }
    return res
  }

  const newMenu: IndexObj[] = []

  menu.forEach((item) => {
    const res = deepFilter(item)
    if (res) {
      newMenu.push(res)
    }
  })

  return newMenu
}

/**
 * 根据用户权限进行路由的过滤
 * @param permissions 用户权限数组
 * @param menu 原始的路由数组
 * @returns 过滤后的权限数组
 */
export const filterAdminRouter = (permissions: string[], menu: IndexObj[]) => {
  const isPass = (auth: string[]) => {
    if (auth === undefined) {
      return true
    }

    return permissions.some((item) => {
      return auth.includes(item)
    })
  }

  const deepFilter = (menuItem: IndexObj) => {
    let res = null

    let auth = undefined
    if (menuItem.meta) {
      auth = menuItem.meta.auth
    }

    if (isPass(auth)) {
      res = menuItem
      // 看是否存在children
      if (menuItem.children && menuItem.children.length > 0) {
        const arrMenu: IndexObj[] = []
        menuItem.children.forEach((item: IndexObj) => {
          const resMenu = deepFilter(item)
          if (resMenu) {
            arrMenu.push(resMenu)
          }
        })
        menuItem.children = arrMenu
      }
    }
    return res
  }

  const newMenu: IndexObj[] = []

  menu.forEach((item) => {
    const res = deepFilter(item)
    if (res) {
      newMenu.push(res)
    }
  })

  return newMenu
}

// 后台管理左侧菜单数组
export const adminMenu = [
  {
    id: 1,
    title: '概览',
    name: 'overview',
    auth: ['admin.index.dashborad'],
    icon: 'statistics-filled',
    level: 1
  },
  {
    id: 2,
    title: '系统设置',
    icon: 'shezhi',
    auth: ['admin.index.setting'],
    level: 1,
    children: [
      {
        id: 3,
        title: '基础设置',
        name: 'settings',
        icon: 'yinliangtiaojie',
        level: 2
      },
      {
        id: 4,
        title: '通知管理',
        name: 'notice',
        icon: 'icon_notice',
        level: 2
      }
    ]
  },
  {
    id: 5,
    title: '部门与成员管理',
    icon: 'qunzu',
    level: 1,
    auth: ['admin.role.list', 'admin.dep.list', 'admin.sourceAuth.list'],
    children: [
      {
        id: 6,
        title: '部门及用户',
        name: 'user',
        icon: 'qunzu',
        auth: ['admin.dep.list'],
        level: 2
      },
      {
        id: 7,
        title: '系统权限管理',
        name: 'role',
        icon: 'jiaosequanxian',
        auth: ['admin.role.list'],
        level: 2
      },
      {
        id: 8,
        title: '文档权限管理',
        name: 'auth',
        icon: 'quanxiansuo',
        auth: ['admin.sourceAuth.list'],
        level: 2
      }
    ]
  },
  {
    id: 9,
    title: '存储/文件',
    icon: 'wenjianjia1',
    level: 1,
    auth: ['admin.storage.list'],
    children: [
      {
        id: 10,
        title: '存储管理',
        name: 'storageOrigin',
        icon: 'cunchushebei',
        level: 2
      },
      {
        id: 11,
        title: '备份管理',
        name: 'backup',
        icon: 'icon_cloudnode_fill',
        level: 2
      },
      {
        id: 12,
        title: '分享管理',
        name: 'share',
        icon: 'rizhi',
        level: 2
      }
    ]
  },
  {
    id: 13,
    title: '日志管理',
    icon: '24gf-shieldCheck',
    level: 1,
    auth: ['admin.index.loginLog', 'admin.index.log'],
    children: [
      // {
      //   id: 14,
      //   title: '消息预警',
      //   icon: 'gaojing',
      //   name: 'warning',
      //   auth: ['admin.index.setting'],
      //   level: 2
      // },
      {
        id: 15,
        title: '登录日志',
        name: 'loginLog',
        icon: 'denglujilu',
        auth: ['admin.index.loginLog'],
        level: 2
      },
      {
        id: 16,
        title: '操作日志',
        name: 'log',
        icon: 'yonghurizhi1',
        auth: ['admin.index.log'],
        level: 2
      }
    ]
  },
  {
    id: 17,
    title: '服务器',
    icon: 'fuwuqi-fill',
    auth: ['admin.schedule.list'],
    level: 1,
    children: [
      {
        id: 18,
        title: '计划任务',
        name: 'scheduleTask',
        icon: 'jihuarenwu1',
        auth: ['admin.schedule.list'],
        level: 2
      }
    ]
  }
]
